<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of candidate_monitoring_activities
 *
 * @author root
 */
class CandidateMonitoringActivitiesController extends AppController {

    //put your code here
    var $name = "CandidateMonitoringActivities";

    public function index($monitoring_activity_id) {
        $this->layout = "ajax";

        //obtengo el listado con todos los id de los aspirantes que aparecen en la resolución.
        $operating_plan_id = $this->CandidateMonitoringActivity->MonitoringActivity->field('MonitoringActivity.operating_plan_id', array('MonitoringActivity.id' => $monitoring_activity_id));
        $proyect_id = $this->CandidateMonitoringActivity->MonitoringActivity->OperatingPlan->field('OperatingPlan.proyect_id', array('OperatingPlan.id' => $operating_plan_id));

        $aspirantes = $this->CandidateMonitoringActivity->Candidate->find('all', array('conditions' => array('Candidate.proyect_id' => $proyect_id, 'Candidate.jerarquia !=' => 0, 'Candidate.estado_filtro' => array(1, 5, 6), 'Candidate.renuncio' => 0), 'fields' => array('Candidate.id')));

        //busco si ya existe un registro para este aspirante en esta visita, si no existe lo creo.
        foreach ($aspirantes as $aspirante) {
            $count = $this->CandidateMonitoringActivity->find('count', array('conditions' => array('CandidateMonitoringActivity.monitoring_activity_id' => $monitoring_activity_id, 'CandidateMonitoringActivity.candidate_id' => $aspirante['Candidate']['id'])));
            if ($count < 1) {
                $insertar = array();
                $insertar['CandidateMonitoringActivity']['candidate_id'] = $aspirante['Candidate']['id'];
                $insertar['CandidateMonitoringActivity']['monitoring_activity_id'] = $monitoring_activity_id;

                $this->CandidateMonitoringActivity->create();
                $this->CandidateMonitoringActivity->save($insertar);
            }
        }

        $listado = $this->CandidateMonitoringActivity->find('all', array('conditions' => array('CandidateMonitoringActivity.monitoring_activity_id' => $monitoring_activity_id), 'fields' => array('CandidateMonitoringActivity.*', 'Candidate.1er_nombre', 'Candidate.2do_nombre', 'Candidate.1er_apellido', 'Candidate.2do_apellido', 'Candidate.id')));

        $this->set('listado', $listado);
        $this->set('operating_plan_id', $operating_plan_id);
    }

    public function edit($id) {
        //No se envian datos para guardar.
        $this->set('monitoring_activity_id', $this->CandidateMonitoringActivity->field('CandidateMonitoringActivity.monitoring_activity_id', array('CandidateMonitoringActivity.id' => $id)));

        if (empty($this->data)) {
            $this->CandidateMonitoringActivity->recursive = 1;
            $this->data = $this->CandidateMonitoringActivity->find("first", array("conditions" => array("CandidateMonitoringActivity.id" => $id), 'fields' => array('CandidateMonitoringActivity.*', 'Candidate.1er_nombre', 'Candidate.2do_nombre', 'Candidate.1er_apellido', 'Candidate.2do_apellido')));
        } else {
            if ($this->CandidateMonitoringActivity->saveAll($this->data)) {
                $this->Session->setFlash('Guardado con éxito', 'flash_custom');
                $this->redirect(array('controller' => 'CandidateMonitoringActivities', 'action' => 'index', $this->data['CandidateMonitoringActivity']['monitoring_activity_id']));
            }
        }
    }

}

?>
